package com.dsbj.mp.icardservice.utils.picprocess;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 二维码处理类。
 * @author cjw 2020-02-18
 */
public class QrcodeProcess {
	
	public static Logger logger = LoggerFactory.getLogger(QrcodeProcess.class);
	
	
	
	/**
	 * 根据infoStr,rgb, 生成对应的二维码字符串。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-02-18
	 */
	public static String generateQrcodeBase64(String infoStr, int rgb) {
		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
		hints.put(EncodeHintType.MARGIN, 1);  //设置白边
		BitMatrix bitMatrix = null;
		String qrcodeBase64Str = null;
		try {
			bitMatrix = multiFormatWriter.encode(infoStr, BarcodeFormat.QR_CODE, 200, 200, hints);
			BufferedImage image = toBufferedImage(bitMatrix, rgb);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			//输出二维码图片流
			ImageIO.write(image, "png", outputStream);
			qrcodeBase64Str = Base64.getEncoder().encodeToString(outputStream.toByteArray());
			return qrcodeBase64Str;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("产生二维码字符串出错!" + e.toString());
		}
		return null;
	}
	
	/**
	 * 根据infoStr,widthPixel,rgb,生成对应的二维码字符串。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-02-18
	 */
	public static String generateQrcodeBase64(String infoStr, int widthPixel, int rgb) {
		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map hints = new HashMap();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
		hints.put(EncodeHintType.MARGIN, 1);  //设置白边
		BitMatrix bitMatrix = null;
		String qrcodeBase64Str = null;
		try {
			bitMatrix = multiFormatWriter.encode(infoStr, BarcodeFormat.QR_CODE, widthPixel, widthPixel, hints);
			BufferedImage image = toBufferedImage(bitMatrix, rgb);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			//输出二维码图片流
			ImageIO.write(image, "png", outputStream);
			qrcodeBase64Str = Base64.getEncoder().encodeToString(outputStream.toByteArray());
			return qrcodeBase64Str;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("产生二维码字符串出错!" + e.toString());
		}
		return null;
	}
	
	/**
	 * 方法来自ReservationService类。
	 *
	 * @param
	 * @return
	 */
	public static BufferedImage toBufferedImage(BitMatrix matrix, int rgb) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				//0xFF000000黑; 0xFFFFFFFF白
				image.setRGB(x, y, matrix.get(x, y) ? rgb : 0xFFFFFFFF);
			}
		}
		return image;
	}
	
	
}
